package com.foreveredu.propms.persist.hibernate;

import java.util.List;

import org.hibernate.Query;

import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.domain.Owner;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.persist.IHouseDao;

public class HibernateHouseDao extends HibernateAbstractDao<House, Integer> implements IHouseDao {

	private static final long serialVersionUID = 1L;
	
	public HibernateHouseDao() {
		super(House.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public House findByHouseId(String houseId) {
		List<House> houseList = this.getHibernateTemplate().find("from House h where h.houseId= ?", houseId);
		House house = null;
		if (houseList != null && houseList.size() > 0) {
			house = houseList.get(0);
		}
		return house;
	}

	//分页查询房产，districtId为0，代表显示所有房产，否则，代表某物业服务处下的所有房产
	@Override
	public PageModel findHouseList(int offset, int districtId) {
		/*
		 * 旧版翻页查询
		 * 
		 * List<House> houseList = null;
		if (districtId == 0) {
			houseList = this.getHibernateTemplate().find("from House h order by h.id");
		} else {
			houseList = this.getHibernateTemplate().find("from House h where h.building.subDistrict.district.id=? order by h.id",districtId);
		}
		PageModel pm = new PageModel();
		if (houseList != null && houseList.size() > 0) {
			pm.setTotal(houseList.size());
			if (offset + 10 > houseList.size()) {
				pm.setDatas(houseList.subList(offset, houseList.size()));
				return pm;
			}
			pm.setDatas(houseList.subList(offset, offset + 10));
		}*/
		PageModel pm = new PageModel();
		if(districtId == 0){
			Query query = this.getSession().createQuery("select count(*) from House");
			Object count = query.uniqueResult();
			if (count != null) {
				int total = Integer.parseInt(count.toString());
				pm.setTotal(total);
			}

			if (offset + 10 > pm.getTotal()) {
				//如果查询的数据大于总记录数，则反过来查询数据，倒叙排列，并且第一页就查询完成
				Query query2 = this.getSession().createQuery("from House as h order by h.houseId");
				query2.setFirstResult(offset);
				query2.setMaxResults(offset + 10 - pm.getTotal());
				pm.setDatas(query2.list());
			}
			Query query3 = this.getSession().createQuery("from House as h order by h.houseId");
			query3.setFirstResult(offset);//设置从那一项开始
			query3.setMaxResults(10);//一共有十项
			pm.setDatas(query3.list());
			
		}else{
			Query query = this.getSession().createQuery("select count(*) from House as h where h.building.subDistrict.district.id = ? ");
			query.setInteger(0, districtId);
			Object count = query.uniqueResult();
			if (count != null) {
				int total = Integer.parseInt(count.toString());
				pm.setTotal(total);
			}

			if (offset + 10 > pm.getTotal()) {
				Query query2 = this.getSession().createQuery("from House as h where h.building.subDistrict.district.id = ? order by h.houseId");
				query2.setInteger(0, districtId);
				query2.setFirstResult(offset);
				query2.setMaxResults(offset + 10 - pm.getTotal());
				pm.setDatas(query2.list());
			}
			Query query3 = this.getSession().createQuery("from House as h where h.building.subDistrict.district.id = ? order by h.houseId" );
			query3.setInteger(0, districtId);
			query3.setFirstResult(offset);
			query3.setMaxResults(10);
			pm.setDatas(query3.list());
		}
		return pm;
	}

	//查找某小区中已注册和登记的房产
	@SuppressWarnings("unchecked")
	@Override
	public List<House> findBySubDistrict(SubDistrict subDistrict) {
		String hql = "from House h where h.building.subDistrict.id=? and h.isReg=1 and h.isInit=1 order by h.houseId";
		List<House> houseList = this.getHibernateTemplate().find(hql, subDistrict.getId());
		
		return houseList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<House> findAllBySubDistrict(SubDistrict subDistrict) {
		String hql = "from House h where h.building.subDistrict.id=? order by h.houseId";
		List<House> houseList = this.getHibernateTemplate().find(hql, subDistrict.getId());
		
		return houseList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public House findhouseByOwner(Owner owner) {
		List<House> houseList = this.getHibernateTemplate().find("from House h where h.owner.id= ?", owner.getId());
		House house = null;
		if (houseList != null && houseList.size() > 0) {
			house = houseList.get(0);
		}
		return house;
	}

	
}
